//
//  YYSettingViewController.swift
//  STMusic
//
//  Created by stoneobs on 17/5/12.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

import UIKit

class YYSettingViewController: STTableViewController {

    let datasouceArray = [["账号与安全"],["新消息提醒","多语言"],["清除所有缓存歌曲","清除所有缓存图片"]];
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "设置";
 
        weak var weakSelf = self;
        let footerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 100));
        footerView.backgroundColor = self.tableView.backgroundColor;
        let logoutButton = STButton.init(frame: CGRect.init(x: 30, y: 0 + 15, width: SCREEN_WIDTH - 60  , height: 40),
                                         title: "退出登录",
                                         titleColor: .white,
                                         titleFont: 13,
                                         cornerRadius: 3,
                                         backgroundColor: UIColor.flatMint,
                                         backgroundImage: nil,
                                         image: nil);
        logoutButton?.contentHorizontalAlignment = .center;
        logoutButton?.centerY = footerView.height / 2;
        logoutButton?.clicAction = { but in
            print("点击了退出登录");
            weakSelf?.loadLogoutRequest();
        }
        footerView.addSubview(logoutButton!);
        self.tableView.tableFooterView = footerView;

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    // MARK: UITableView DataSouce
    override func numberOfSections(in tableView: UITableView) -> Int {
        return datasouceArray.count;
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datasouceArray[section].count;
    }
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44;
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10;
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell");
        if cell == nil {
            cell = UITableViewCell.init(style: .value1, reuseIdentifier: "cell");
        }
        cell?.textLabel?.text = datasouceArray[indexPath.section][indexPath.row];
        cell?.textLabel?.font = Font15;
        cell?.textLabel?.textColor = FirstTextColor;
        return cell!;
    }
    
    // MARK: UITableView Delegate
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false);
        
        if indexPath.section == 0 && indexPath.row == 0 {
            self.navigationController?.pushViewController(YYAccountSafeViewController(), animated: true);
        }
        
    }

        // MARK:Network Request
    func loadLogoutRequest() -> Void {
        self.showAlertCancelAndConfirm("爱卿确定要退出登录吗？") { (name) in
            if name == "确认"{
                BmobUser.logout();
 
                UserDefaults.standard.removeObject(forKey: YYLOGIN_TOKEN);
                UserDefaults.standard.synchronize();
                
                UIApplication.shared.keyWindow?.rootViewController = STNavigationController.init(rootViewController: YYLoginViewController());
                SVProgressHUD.showSuccess(withStatus: "朕以准奏，爱卿领旨谢恩吧")
                STLayoutViewController.defaultLayout().dismissCurrentController(false);
            }
        }
       
    }


}
